package edu.spec.student.controller;

import edu.spec.student.model.Student;
import edu.spec.student.service.iface.ILectureService;
import edu.spec.student.service.iface.IStudentLectureService;
import edu.spec.basic.model.dto.MvcReturn;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by MadFrog on 2017/6/12.
 */
@Controller
@RequestMapping("/admin/studentLecture")
public class StudentLectureController {

    @Inject
    private ILectureService lectureService;
    @Inject
    private IStudentLectureService studentLectureService;

    @ResponseBody
    @RequestMapping("/listStudentLecture")
    public MvcReturn listStudentEventByStudentId(HttpSession session){
        Student currentStu = (Student)session.getAttribute("user");
        Map<String, List> emap = lectureService.listStudentLecture(currentStu.getId());
        return new MvcReturn(true, emap);
    }

    @ResponseBody
    @RequestMapping("/enroll/{lectureId}")
    public MvcReturn enrollEvent( @PathVariable Integer lectureId, HttpSession session){
        Student currentStu = (Student)session.getAttribute("user");
        lectureService.attend(currentStu.getId(), lectureId);
        return new MvcReturn(true);
    }

    @RequestMapping("/signIn")
    public String signIn(Integer lectureId, Float latitude, Float longitude, HttpSession session){
        Student currentStu = (Student)session.getAttribute("user");
        studentLectureService.signIn(currentStu.getId(), lectureId, latitude, longitude);
        return "common/optSuc";
    }

    /**
     * 学生对讲座进行点赞
     * @param lectureId 讲座主键
     * @param session   用户请求对应的session
     * @return
     */
    @RequestMapping("/like/{lectureId}")
    public String like(@PathVariable Integer lectureId, HttpSession session){
        Student currentStu = (Student)session.getAttribute("user");
        studentLectureService.like(currentStu.getId(), lectureId);
        return "common/optSuc";
    }

    @RequestMapping("/exportOut")
    public void exportOut(Date startTime, Date endTime, HttpServletRequest req, HttpServletResponse resp){
//        List<Student> studentList = studentEventService.listAllStudentByEvent(eventId);
//        Event event = lectureService.load(eventId);
//        ServletOutputStream fout = null;
////        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-HH-mm");
//        String filename = URLEncoder.encode(event.getName() + "情况导出");
//        try {
//            if (req.getHeader("user-agent").indexOf("MSIE") != -1) {
//                filename = URLEncoder.encode(filename, "utf-8") + ".xls";
//            } else {
//                filename = new String(filename.getBytes("utf-8"), "iso-8859-1") + ".xls";
//            }
//            resp.setHeader("Content-Disposition", "attachment;filename=" + filename);
//            resp.setContentType("application/msexcel");
//            fout = resp.getOutputStream();
//            XlsTools.exportExcel("导出", Student.class, studentList, fout);
//            fout.flush();
//            fout.close();
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        // 根据时间段，导出全系学生参加讲座次数情况
    }

}